<?php
namespace Modules\Admin\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Admin\Entities\Admin;

use Validator;
use ThrowException;
use Auth;

class LoginController extends Controller
{
    // 密码登陆
    public function loginWithPassword(Request $req)
    {
        $rule = [
            'phone' => ['required'],
            'password' => ['required'],
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $admin = Admin::isExisting([
            'phone' => $req->phone,
            'password' => $req->password
        ]) ?: ThrowException::Conflict('手机号或密码不正确');

        $token = Auth::generateToken($admin, [
            // 'platform' => 'pc_web'
        ]);

        $info = getIpLocation($req->getClientIp());
        \Log::error('登陆地区: ' . implode('-', $info));

        return [
            'token' => $token
        ];
    }
}
